package net.sxnic.workflow.whimpl;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.sxnic.workflow.context.WfContextImpl;
import net.sxnic.workflow.handler.AbstractHandler;
import net.sxnic.workflow.handler.HandlerExt;
import net.sxnic.workflow.handler.WfHandlerException;
import net.sxnic.workflow.wfentity.WfEntity;

/**
 * 验证是否需要接受人和 意见
 * validate 如果没有定义则表示：不需要意见，但是必须有接收人
 * @author 孙宇飞
 *
 */
public class AssignAndCommentValidateHandler extends AbstractHandler<WfEntity> implements HandlerExt<WfEntity>{
	
	private static Logger logger = LoggerFactory.getLogger(AssignAndCommentValidateHandler.class);

	public AssignAndCommentValidateHandler(WfContextImpl<WfEntity> context) {
		super(context);
	}

	@Override
	public void editExecute() throws WfHandlerException {
		
	}

	@Override
	public void saveExecute() throws WfHandlerException {
		
	}

	@Override
	public void viewExecute() throws WfHandlerException {
		
	}

	@Override
	public void submitExecute() throws WfHandlerException {
		String validate = context.getTask().getNode().getValidate();
		logger.debug("===validate definition===="+validate);
		//没定义 则必须有接收人
		if(StringUtils.isBlank(validate)){
			if(StringUtils.isBlank(context.getSelectedNextAssignees())){
				context.setErrorMsg("请选择至少一个办理人！");
				throw new WfHandlerException("请选择至少一个办理人！");
			}
		}
		
		String[] v_s = StringUtils.split(validate, ";");
		String before="";
		String after="";
		
		for(String v:v_s){
			before = StringUtils.substringBefore(v, ":");
			after = StringUtils.substringAfter(v, ":");
			if("rquired".equals(after)){
				if("comment".equals(before)){
					if(StringUtils.isBlank(context.getTaskComment())){
						context.setErrorMsg("请填写办理意见之后，再签字发送！");
						throw new WfHandlerException("请填写办理意见之后，再签字发送！");
					}
				}
			}
		}
	}

}
